package org.cy.shixun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.cy.shixun.common.CustoException;
import org.cy.shixun.entity.Cart;
import org.cy.shixun.entity.Goods;
import org.cy.shixun.mapper.CartMapper;
import org.cy.shixun.mapper.GoodsMapper;
import org.cy.shixun.service.CartService;
import org.cy.shixun.utils.RedisUtil;
import org.cy.shixun.dto.CartShowDto;
import org.cy.shixun.vo.ProductShow;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author TrainingL
 * @since 2023-06-29
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private CartMapper cartMapper;

    @Override
    public int join(ProductShow productShow, String token) {
        //获取我的账号
        Object account = redisUtil.getString(token);
        //Object account="admin";
        //获取卖家账号和商品名称
        Goods goods = goodsMapper.selectById(productShow.getGoodsId());
        if(account==null||goods==null){
            throw new CustoException("用户账号异常!");
        }
        String clientsAccount=goods.getClientsAccount();
        String goodsName=goods.getGoodsName();

        BigDecimal money=productShow.getPrice().multiply(BigDecimal.valueOf(productShow.getGoodsNum()));

        //判断cart表中是否已有该商品，有更新，没有新增
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getGoodsId,productShow.getGoodsId())
                .eq(Cart::getAccount,account)
                .eq(Cart::getGoodsSize,productShow.getSize());
        Cart selectOne = cartMapper.selectOne(queryWrapper);
        int res;
        if(selectOne!=null&&selectOne.getGoodsSize().equals(productShow.getSize())){
            selectOne.setWantedNum(selectOne.getWantedNum()+productShow.getGoodsNum());
            selectOne.setGoodsMoney(selectOne.getGoodsMoney()
                    .multiply(BigDecimal.valueOf(selectOne.getWantedNum())));
            res= cartMapper.updateById(selectOne);
        }else{
            //插入数据
            Cart cart = new Cart();
            cart.setGoodsId(productShow.getGoodsId());
            cart.setClientsAccount(clientsAccount);
            cart.setAccount((String) account);
            cart.setWantedNum(productShow.getGoodsNum());
            cart.setGoodsName(goodsName);
            cart.setGoodsSize(productShow.getSize());
            cart.setGoodsMoney(money);
            try {
                res = cartMapper.insert(cart);
            }catch (Exception e){
                throw new CustoException("未知错误");
            }
        }
        return res;
    }


    @Override
    public List<CartShowDto> cartShow(String account) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getAccount,account);
        List<Cart> carts = cartMapper.selectList(queryWrapper);
        List<CartShowDto> cartShowDtoList=new ArrayList<>();
        //如果用户的购物车有商品
        if(carts.size()>0){
            //转换成dto
            cartShowDtoList = carts.stream().map(item -> {
                CartShowDto cartShowDto = new CartShowDto();
                BeanUtils.copyProperties(item, cartShowDto);
                //获取商品单价
                BigDecimal goodsValue = item.getGoodsMoney()
                        .divide(BigDecimal.valueOf(item.getWantedNum()));
                cartShowDto.setGoodsName(item.getGoodsName()+"+"+item.getGoodsSize());
                cartShowDto.setGoodsValue(goodsValue);
                return cartShowDto;
            }).collect(Collectors.toList());
        }
        return cartShowDtoList;
    }


    @Override
    public void deleteGoods(int goodsId, String account,String goodsSize) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getGoodsId,goodsId)
                .eq(Cart::getAccount,account)
                .eq(Cart::getGoodsSize,goodsSize);
        cartMapper.delete(queryWrapper);
    }
}
